
<template>
  <div>
    <!--预览图片
       :on-preview="preview"
     -->
    <!-- action上传地址 -->
    <el-upload
      list-type="picture-card"
      :limit="1"
      action="#"
      :file-list="fileList"
      :class="{ disabled: fileComputed }"
      :on-preview="preview"
      :on-change="changeFile"
      :on-remove="handleRemove"
      :before-upload="beforeUpload"
      :http-request="upload"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <!-- 进度条 -->
    <el-progress v-if="showPercent" style="width:180px" :percentage="percent" />
    <el-dialog :visible.sync="showDialog" title="上传图片">
      <img :src="imgUrl" style="width: 100%">
    </el-dialog>
  </div>
</template>

<script>
// 腾讯云cos包
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDzrWw74nIYaNxTlpfLn11mHXaS7XigS0L',
  SecretKey: 'B8rLbcOvDGI6fz5vVpsfCTUVKuFMHzSh'
})

export default {
  data() {
    return {
      fileList: [
        {
          url: 'http://pic.51yuansu.com/backgd/cover/00/11/45/5b77558bc0687.jpg!/fw/780/quality/90/unsharp/true/compress/true'
        }
      ], // 图片地址设置为数组
      showDialog: false, // 控制显示弹层
      imgUrl: '',
      currentFileUid: null, // 记录当前正在上传的uid
      percent: 0, /* 记录当前的百分比 */
      showPercent: false // 进度条显示
    }
  },

  computed: {
    fileComputed() {
      return this.fileList.length === 1
    }
  },
  methods: {
    preview(file) {
      // file 文件地址
      console.log(file)
      // 图片地址
      console.log(file.url)
      this.imgUrl = file.url
      this.showDialog = true
      console.log(11)
    },
    // 删除文件  file是要删除的文件  fileList删除后的文件
    handleRemove(file, fileList) {
      console.log(112)
      console.log(file)
      console.log(fileList)
      console.log(this.fileList)
      this.fileList = this.fileList.filter(item => item.uid !== file.uid)
    },
    // 添加文件  file本地地址 当前的文件  fileList 最新数组
    changeFile(file, fileList) {
      console.log(111)
      console.log(file)
      console.log(fileList)
      console.log(this.fileList)
      console.log(fileList.length)
      this.fileList = fileList.map(item => item)
      console.log(this.fileList)
    },
    beforeUpload(file) {
      console.log(file)
      // 检查文件类型
      const types = ['image/jpeg', 'image/gif', 'image/bmp', 'image/png', 'image/mp4']
      if (!types.some(item => item === file.type)) {
        console.log(111111)
        this.$message.error('上传图片格式错误')

        // 上传终止
        return false
      }
      // 检查文件大小
      const maxSize = 10 * 1024 * 1024
      if (file.size > maxSize) {
        console.log(file)
        this.$message.error('大小不能超过5M')
        return false
      }

      // 确认上传
      console.log(file)
      this.currentFileUid = file.uid
      this.showPercent = false
      return true
    },
    // 覆盖默认的上传行为，可以自定义上传的实现
    upload(params) {
      console.log(params.file)
      console.log(params)
      if (params.file) {
        // 执行上传操作
        cos.putObject({
          Bucket: 'lss-26-1306674891', /* 必须 存储桶名称*/
          Region: 'ap-nanjing', /* 存储桶所在地域，必须字段 */
          Key: 'params.file', /* 必须 文件名*/
          StorageClass: 'STANDARD', /* 上传模式 */
          Body: params.file, // 上传文件对象
          onProgress: (params) => {
            this.percent = params.percent * 100
            console.log(JSON.stringify(params))
          }
        }, (err, data) => {
          console.log(err || data)
          if (!err && data.statusCode === 200) {
            this.fileList = this.fileList.map(item => {
              if (item.uid === this.currentFileUid) {
                return { url: 'http://' + data.Location, upload: true }
              }
              return item
            })

            setTimeout(() => {
              // 关闭进度条
              this.showPercent = false
              // 重置百分比
              this.percent = 100
            }, 1000)
          }
        })
      }
    }

  }

}
</script>
<style lang="scss" scoped>
.disabled .el-upload-list--picture-card {
  display: none;
}
</style>
